package service.edu.controller.index;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import service.edu.entity.Course;
import service.edu.entity.Teacher;
import service.edu.entity.vo.IndexCourseAll;
import service.edu.service.CourseService;
import service.edu.service.TeacherService;
import service.edu.result.R;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/edu/index")
public class IndexController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 用sql语句限制课程按播放量的高低查询前8个,舰娘根据id查询4个
     */

    @ApiOperation("前台首页查询课程")
    @GetMapping("indexQueryCourse")
    @Cacheable(value = "course",key = "'courseList'")
    public R indexQueryCourse(){
        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.orderByDesc("view_count").last("limit 8");
        List<Course> courseList = courseService.list(courseWrapper);
        return R.Ok().data("courseList", courseList);
    }

    @ApiOperation("前台首页查询舰娘")
    @GetMapping("indexQueryTeacher")
    @Cacheable(value = "jianniang",key = "'jianniangList'")
    public R indexQueryTeacher(){
        QueryWrapper<Teacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("id").last("limit 4");
        List<Teacher> teacherList = teacherService.list(teacherWrapper);
        return R.Ok().data("teacherList",teacherList);
    }

    @ApiOperation("根据课程类似名查询课程")
    @GetMapping("queryCourse/{courseName}")
    public R getCourseByLikeCourseName(@PathVariable String courseName){
         String courseId= courseService.queryCourseByLikeName(courseName);
        return R.Ok().data("courseId", courseId);
    }
}
